/*--------------------------------------------------
* @Description:     问卷管理详情                     *
* @Version:         1.0.0                           *
* @author:          sunwanlin (1124038074@qq.com)  *
* @date             2017.11.5                      *
---------------------------------------------------*/
$(function(){
/*********************************************函数调用********************************************************************/
    /*
        发送详情页id
    */
    sendId();
/*********************************************事件绑定********************************************************************/

/*********************************************函数声明********************************************************************/
    /*
        获取详情页id
    */
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]); return null; //返回参数值
    }
    /*
        发送详情页id
    */
    function sendId(){ 
        var id = $(".hidId").val();
        $.ajax({
            type: jQuery.url.QuestionnaireManagement.type,
            url: jQuery.url.QuestionnaireManagement.sendDetailId.url,
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            data: {
               id : id 
            },     //JSON.stringify
            dataType: "json",
            success: function (rs) {
                if( rs.code == 0){ 
                    detailData(rs);//详情页刷问卷数据
                    appendOption(rs);//详情页刷选项数据
                    setWidth(rs);//set进度条里边颜色的长度
                }
            },
            error: function (rs) {
                
            }
        });
    }
    /*
        详情页刷问卷数据
    */
    function detailData(rs){
        var dom,
            str='';

        $('.panel-body').empty();

        dom = ' <form class="form-horizontal J_form" method="" action="">\
                        <input type="hidden" name="clientId" value="1">\
                        <div class="form-group">\
                            <label class="col-sm-2 control-label">问卷标题:</label>\
                            <div class="col-sm-4">\
                                <input class="new500 J_title border0" type="text" name="title" value="" readonly="readonly"/>\
                            </div>\
                        </div>\
                        <div class="form-group">\
                            <label class="col-sm-2 control-label">问卷截止时间:</label>\
                            <div class="col-sm-9">\
                                <input id="endTime" class="new500 J_endTime border0" type="text" name="endTime" value="" readonly="readonly"/>\
                            </div>\
                        </div>\
                        <div class="form-group">\
                            <label class="col-sm-2 control-label">问卷权限:</label>\
                            <div class="col-sm-4">\
                                <input class="new500 newHeight80 J_content border0" type="text" name="content" value="" readonly="readonly" />\
                            </div>\
                        </div>  \
                        <div class="form-group">\
                            <label class="col-sm-2 control-label">问卷人数:</label>\
                            <div class="col-sm-4">\
                                <input class="new500 newHeight80 J_count border0" type="text" name="count" value="" readonly="readonly"/>\
                            </div>\
                        </div>\
                    </form>';
        $('.panel-body').append(dom);
        /*这四行是给问卷公共部分从json里赋值*/
        $('.J_title').val(rs.quest[0].title);
        $('.J_endTime').val(rs.quest[0].endTime);
        $('.J_content').val(rs.quest[0].role);
        $('.J_count').val(rs.quest[0].count);

        $.each(rs.list, function(index, item){
            str += '  <div class="panel panel-info box">\
                        <div class="top">\
                            <h4 class="top-left">'+ item.questionName +'</h4>\
                        </div>\
                        <div class="panel-body panel-question"><!-- panel-question指问题panel，里边的子panel -->\
                            <form class="form-horizontal J_optionForm" method="" action="">\
                                <div class="form-group option-panel">\
                                    \
                                </div>\
                            </form>\
                        </div>\
                    </div>';
                    
        });
        $('.panel-body').append(str);
    }
    /*
        刷详情页选项数据
    */
    function appendOption(rs){
        
        $.each(rs.list, function(index, item){
            var optionStr='';
            $.each(item.option , function(optionIndex, optionItem){
                optionStr += '<label class="option-show col-sm-4 col-sm-offset-1">\
                    '+ optionItem.name +'\
                    <div class="percent-show"><span>'+ optionItem.percent +'</span>%</div>\
                    <div class="count-show"><span>'+ optionItem.count +'</span>票</div>\
                </label>\
                <div class="progress-out col-sm-4 col-sm-offset-1">\
                    <div class="progress-in"></div>\
                </div>\
                <br/>';
            }); 
            $('.option-panel').eq(index).append(optionStr);
        }); 
    }
    /*
        set进度条里边颜色的长度
    */
    function setWidth(rs){
        var progressOutWidth = $('.progress-out').css('width'),
            progressInWidth;

        $.each(rs.list, function(index, item){
            $.each(item.option , function(optionIndex, optionItem){
                progressInWidth = parseInt(progressOutWidth) * (optionItem.percent/100);
                $('.option-panel').eq(index).find('.progress-in').eq(optionIndex).css('width',progressInWidth);
            });
        });
    }
});